#!/bin/bash
echo "post inst"

DATAPATH=/var/lib/opengauss
APPPATH=/usr/local/opengauss
USERNAME=opengauss
GROUPNAME=opengauss

GAUSSHOME=${APPPATH}
PGDATA=/var/lib/opengauss/data
PGPORT=7654
PGDATABASE=postgres

filename=$(ls ${APPPATH}/openGauss*)

tar -xf ${filename} -C ${APPPATH}
chmod -R 755 ${APPPATH}

## create user

group_name=opengauss
user_name=opengauss

/usr/sbin/groupadd -r ${GROUPNAME} >/dev/null 2>&1 || :
/usr/sbin/useradd -M -N -g opengauss -r -d ${DATAPATH} -s /bin/bash -c "openGauss Server" ${USERNAME} >/dev/null 2>&1 || :

## initdb
cp ${APPPATH}/opengauss.bash_profile ${DATAPATH}/.bash_profile
chown -R ${USERNAME}:${GROUPNAME} ${DATAPATH}

echo "Begin to init a database data dir"
result=$(su - opengauss -c "export GAUSSHOME=${GAUSSHOME};export LD_LIBRARY_PATH=${GAUSSHOME}/lib; ${GAUSSHOME}/bin/gs_initdb -D ${PGDATA} -U ${USERNAME} --nodename=single_node")
if [ $? -ne 0 ]; then
    echo "Init openGauss database failed."
    exit $result
else
    echo "Init openGauss database success."
    echo "Start database with command: \"service opengauss start\""
fi

